package com.sywb.chuangyebao.library.player.adsorber;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.sywb.chuangyebao.library.player.AliPlayerView;
import com.sywb.chuangyebao.library.player.TXPlayerView;

public class FloatView extends FrameLayout {

    //阿里播放器
    private AliPlayerView mVideoPlayView;
    //腾讯播放器
    private TXPlayerView mTXPlayerView;

    private int playerType;

    public FloatView(@NonNull Context context) {
        this(context, null);
    }

    public FloatView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FloatView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void init(int playerType) {
        this.playerType = playerType;

        FrameLayout videoRoot = new FrameLayout(getContext());
        switch (playerType) {
            case 1:
                mVideoPlayView = new AliPlayerView(getContext());
                videoRoot.addView(mVideoPlayView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                break;
            case 2:
                mTXPlayerView = new TXPlayerView(getContext());
                videoRoot.addView(mTXPlayerView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                break;
        }

        FrameLayout rootLayout = new FrameLayout(getContext());
        rootLayout.addView(videoRoot, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        //NOTE: root layout'heihgt must be WRAP_CONTENT
        addView(rootLayout, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public View getVideoRootView() {
        switch (playerType) {
            case 1:
                return (View) mVideoPlayView.getParent();
            case 2:
                return (View) mTXPlayerView.getParent();
            default:
                return null;
        }
    }

    public View getVideoPlayView() {
        switch (playerType) {
            case 1:
                return mVideoPlayView;
            case 2:
                return mTXPlayerView;
            default:
                return null;
        }
    }
}
